package com.example.uniapp.activity.frament;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.uniapp.R;
import com.example.uniapp.activity.main.MainUserActivity;
import com.example.uniapp.activity.userHome.UserCookbookActivity;
import com.example.uniapp.activity.userHome.UserUpdateInfoActivity;
import com.example.uniapp.activity.userHome.UserUpdatePwdActivity;
import com.example.uniapp.bean.UserBean;
import com.example.uniapp.dao.UserDao;
import com.example.uniapp.utils.Tools;

public class UserPersonCenterFragment extends Fragment {

    View rootview; //根视图

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstancesState) {
        rootview = inflater.inflate(R.layout.fragment_user_person_center, container, false);

        //加载个人信息
        String account = Tools.getOnAccount(getContext());
        //查询该账号相关信息
        UserBean user = UserDao.getUserInfoByAccount(account);

        //设置头像
        ImageView userImg = rootview.findViewById(R.id.user_person_center_img);
        Bitmap imgBitmap = BitmapFactory.decodeFile(user.getUserImg());
        userImg.setImageBitmap(imgBitmap);

        //设置用户名
        TextView userName = rootview.findViewById(R.id.user_person_center_user_name);
        userName.setText(user.getUserId());

        //获取父界面
        MainUserActivity activity = (MainUserActivity) getActivity();

        //退出账号
        TextView exitTextView = rootview.findViewById(R.id.user_person_center_exit_textview);
        exitTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //清空共享的数据信息
                SharedPreferences sharedPreferences = getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("account", null);
                editor.apply();

                //退出
                activity.finish();
            }
        });

        //修改个人信息
        TextView updateInfoTextView = rootview.findViewById(R.id.user_person_center_update_info_textview);
        updateInfoTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到修改个人信息界面
                Intent intent = new Intent(getActivity(), UserUpdateInfoActivity.class);
                startActivity(intent);
            }
        });

        //修改密码
        TextView updatePwdTextView = rootview.findViewById(R.id.user_person_center_update_pwd_textview);
        updatePwdTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到修改密码界面
                Intent intent = new Intent(getActivity(), UserUpdatePwdActivity.class);
                startActivity(intent);
            }
        });

        //修改已发布食谱信息
        TextView updateCookbookTextView = rootview.findViewById(R.id.user_person_center_update_cookbook_textView);
        updateCookbookTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到查看自己发布的食谱信息的界面
                Intent intent = new Intent(getActivity(), UserCookbookActivity.class);
                startActivity(intent);
            }
        });
        return rootview;
    }
}